package panel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.util.ArrayList;
import libs.ImageProcess;

public class PanelMy extends JPanel {

    private static final long serialVersionUID = 1L;
    private boolean img_flag = false;
    private Image img;  //  @jve:decl-index=0:
    private String text = "";  //  @jve:decl-index=0:
    private boolean resize = false;
    private String msg = "";
    private boolean isSignatureBehaviourActive = false;
    private int[][] sign;
    private Point pos1;
    private Point pos2;
    public JTextField x1;
    public JTextField y1;
    public JTextField x2;
    public JTextField y2;
    public JLabel sizeLabel;
    private int imageWidth;
    private int imageHeight;
    private Rectangle roiPos = new Rectangle();
    private Integer roiSize;
    private Point mousePos;
    public ArrayList<Rectangle> processedROIList = new ArrayList<Rectangle>();
    private JLabel lbl = null;
    private boolean lblShow = false;
    private int[][] virtualData = null;
    private int[][] realData = null;

    /**
     * This is the default constructor
     */
    public PanelMy() {
        super();
        initialize();
        this.img_flag = true;
    }

    public PanelMy(boolean flag) {
        super();
        initialize();
        this.img_flag = flag;
    }

    public PanelMy(String msg) {
        super();
        initialize();
        this.msg = msg;
    }

    public PanelMy(Image img) {
        this.img = img;
        setLayout(null);
        this.img_flag = true;
    }

    private void initialize() {
        this.setSize(215, 183);
        this.setLayout(null);
        this.setBackground(new Color(102, 102, 255));  // Generated
        lbl = new JLabel("merhaba");
        this.add(lbl);
        lbl.setBounds(new Rectangle(10, 10, 200, 40));
        lbl.setBackground(Color.yellow);
        lbl.setForeground(Color.red);
        lbl.setVisible(true);
        this.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        this.updateUI();

        this.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent e) {
                lblShow = true;
                pos1 = e.getPoint();
                showMouseCoordinates();
//				calculateRealPos();
//				showToolTipText("Mouse pressed:(x,y) "+e.getX()+":"+e.getY());
            }

            public void mouseReleased(java.awt.event.MouseEvent e) {
                lblShow = false;
                pos2 = e.getPoint();
                showMouseCoordinates();
//				calculateRealPos();
// 			    showToolTipText("Mouse released:(x,y) "+e.getX()+":"+e.getY());
            }
        });
        this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseMoved(java.awt.event.MouseEvent e) {
                //System.out.println("mouseMoved()"); // TODO Auto-generated Event stub mouseMoved()
            }

            public void mouseDragged(java.awt.event.MouseEvent e) {
                lblShow = true;
                pos1 = e.getPoint();
                showMouseCoordinates();

//				pos2=e.getPoint();
//				showToolTipText("("+pos1.x+":"+pos1.y+")-("+pos2.x+":"+pos2.y+")");
//				showToolTipText("");
//				updateExternalJTextFieldParams();
//				calculateRealPos();
            }
        });
    }

    protected void showMouseCoordinates() {
        repaint();
    }

    protected void calculateRealPos() {
        double aw = (imageWidth * 1.0) / (this.getWidth() * 1.0);
        double ah = (imageHeight * 1.0) / (this.getHeight() * 1.0);
        mousePos.x = (int) Math.round(aw * pos1.x);
        mousePos.y = (int) Math.round(ah * pos1.y);
//		roiPos.x=mousePos.x;
//		roiPos.y=mousePos.y;
//		roiPos.width=(int)Math.round(aw*pos2.x)-roiPos.x;
//		roiPos.height=(int)Math.round(ah*pos2.y)-roiPos.y;
//		roiSize=roiPos.width*roiPos.height;
        repaint();
    }

    public Rectangle getROIPos() {
        return this.roiPos;
    }

    public Integer getROISize() {
        return this.roiSize;
    }

    public Point getMousePos() {
        return this.mousePos;
    }

    protected void updateExternalJTextFieldParams() {
        double aw = (imageWidth * 1.0) / (this.getWidth() * 1.0);
        double ah = (imageHeight * 1.0) / (this.getHeight() * 1.0);
        if (x1 != null) {
            x1.setText(Math.round(aw * pos1.x) + "");
        }
        if (y1 != null) {
            y1.setText(Math.round(ah * pos1.y) + "");
        }
        if (x2 != null) {
            x2.setText(Math.round(aw * pos2.x) + "");
        }
        if (y2 != null) {
            y2.setText(Math.round(ah * pos2.y) + "");
        }
        if (sizeLabel != null) {
            sizeLabel.setText((Math.round(aw * pos2.x) - Math.round(aw * pos1.x)) * (Math.round(ah * pos2.y) - Math.round(ah * pos1.y)) + "");
        }
        //System.out.println("aw:"+aw+" q:"+Math.round(aw*pos1.x));
    }

    protected void showToolTipText(String text) {
        lbl.setText(text);
        Point p = new Point();
        p.x = pos1.x - 1;
        p.y = pos1.y - 30;
        lbl.setLocation(p);
        this.updateUI();
    }

    public void setImage(Image img) {
        this.img = img;
        this.img_flag = true;
        this.resize = true;
        if (img != null) {
            this.imageWidth = img.getWidth(null);
            this.imageHeight = img.getHeight(null);
            virtualData = ImageProcess.imageToPixels255(img);
            realData = ImageProcess.imageToPixels255(img);
            repaint();
        }
    }

    public void setRealData(int[][] d) {
        this.realData = d;
        repaint();
    }

    public void setImage(Image img, boolean resize) {
        this.img_flag = true;
        this.img = img;
        this.resize = resize;
        repaint();
    }

    public void setImage(Image img, String str) {
        this.img_flag = true;
        this.text = str;
        this.img = img;
        this.resize = true;
        repaint();
    }

    public Image getImage() {
        return this.img;
    }

    public void paint(Graphics g) {
        int dx = 20;
        int dy = 20;
        if (this.img_flag) {
            //super.paintComponents(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.GREEN);
            if (text.equals("")) {
                if (resize) {
                    g.drawImage(img, dx, dy, this.getWidth() - 2 * dx, this.getHeight() - 2 * dy, this);
                } else {
                    g.drawImage(img, 1, 1, this);
                }
                if (msg != "") {
                    g.drawString(msg, 5, this.getHeight() - 5);
                }
            } else {
                g.drawString(text, 5, 13);
                if (resize) {
                    g.drawImage(img, 1, 15, this.getWidth() - 1, this.getHeight() - 15, this);
                } else {
                    g.drawImage(img, 1, 15, this);
                }
            }
            g.setColor(Color.red);
            int w = this.getWidth();
            int h = this.getHeight();
            //System.out.println("w:"+w+" h:"+h);
            g.drawRect(0, 0, w - 1, h - 1);
            g.drawRect(1, 1, w - 3, h - 3);
//			if (isSignatureBehaviourActive) {
//				g.setColor(Color.green);
//				int dw=w/14;
//				int dh=h/256;
//				int px=0;
//				int py=0;
//				for (int i = 0; i < this.sign.length; i++) {
//					//g.fillRect(i*dw, sign[i]*dh,dw, dh);
//					for (int j = 0; j < sign[0].length; j++) {
//						if (sign[i][j]!=0) {
//							g.drawLine(px, py, j*dw, sign[i][j]*dh);
//							px=j*dw;
//							py=sign[i][j]*dh;							
//						}
//					}
//					px=0;
//					py=0;
//				}
//			}
            //b.daha onceden dosyaya kaydedilmis regionlari cizer
//			g.setColor(Color.green);
//			if (processedROIList!=null && processedROIList.size()>0) {
//				double aw=(imageWidth*1.0)/(this.getWidth()*1.0);
//				double ah=(imageHeight*1.0)/(this.getHeight()*1.0);
//				for (int i = 0; i < processedROIList.size(); i++) {
//					Rectangle rect=processedROIList.get(i);
//					int x=(int)Math.round(rect.x/aw);
//					int y=(int)Math.round(rect.y/ah);
//					int wi=(int)Math.round(rect.width/aw);
//					int hi=(int)Math.round(rect.height/ah);
//					g.drawRect(x,y,wi,hi);
//					System.out.println("pos:"+x+","+y+","+wi+","+hi);
//				}
//			}

        }
        if (lblShow && realData != null) {
//        	this.paintComponents(g);
//			g.setColor(Color.green);
//			if (processedROIList!=null && processedROIList.size()>0) {
//				double aw=(imageWidth*1.0)/(this.getWidth()*1.0);
//				double ah=(imageHeight*1.0)/(this.getHeight()*1.0);
//				for (int i = 0; i < processedROIList.size(); i++) {
//					Rectangle rect=processedROIList.get(i);
//					int x=(int)Math.round(rect.x/aw);
//					int y=(int)Math.round(rect.y/ah);
//					int wi=(int)Math.round(rect.width/aw);
//					int hi=(int)Math.round(rect.height/ah);
//					g.drawRect(x,y,wi,hi);
//					System.out.println("pos:"+x+","+y+","+wi+","+hi);
//				}
//			}
//        	g.setColor(Color.red);
//        	g.drawRect(pos1.x,pos1.y,pos2.x-pos1.x,pos2.y-pos1.y);
//        	lbl.setVisible(true);

            if (pos1.x > dx && pos1.x < this.getWidth() - 20 && pos1.y > dy && pos1.y < this.getHeight() - 20) {
                g.setColor(Color.black);
                g.fillRect(pos1.x + 5, pos1.y - 22, 60, 30);
                g.setColor(Color.green);
                g.drawRect(pos1.x + 5, pos1.y - 22, 60, 30);

                int px = getMatrixPX(pos1.x);
                int py = getMatrixPY(pos1.y);

                g.drawString("[" + px + ":" + py + "]", pos1.x + 10, pos1.y - 10);
                g.drawString("" + realData[px][py], pos1.x + 10, pos1.y + 5);
                double qx = (this.getWidth() - 40) * 1.0 / realData.length;
                double qy = (this.getHeight() - 40) * 1.0 / realData[0].length;
                g.drawRect((int) (px * qx) + 20, (int) (py * qy) + 20, (int) qx, (int) qy);
            }
        }
        g.setColor(Color.green);
        if (realData != null) {
            g.drawString("0", 20, 15);
            g.drawString(realData.length + "", this.getWidth() - 40, 15);
            g.drawString("0", 5, 30);
            g.drawString(realData[0].length + "", 5, this.getHeight() - 5);
        }
        g.drawRect(dx - 1, dy - 1, this.getWidth() - 2 * dx + 2, this.getHeight() - 2 * dy + 2);
    }

    private int getMatrixPY(int y) {
        y = y - 20;
        double dy = (this.getHeight() - 40) * 1.0 / realData[0].length;
        return (int) (y / dy);
    }

    private int getMatrixPX(int x) {
        x = x - 20;
        double dx = (this.getWidth() - 40) * 1.0 / realData.length;
        return (int) (x / dx);
    }

    public void setSignature(int[][] sign) {
        this.isSignatureBehaviourActive = true;
        this.sign = sign;
        repaint();
    }

    protected void println(String msg) {
        System.out.println(msg);
    }
}
